package com.makenv.web.portal;

import com.makenv.common.Const;
import com.makenv.domain.User;
import com.makenv.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {
        User user = userService.selectUser(username, password);
        String url = "";
        if (user == null) {
            request.setAttribute("msg", "username and password is wrong");
            url = "forward:/to/login";
        } else {
            HttpSession httpsession = request.getSession();
            httpsession.setAttribute(Const.CURRENT_USER, username);
            url = "redirect:/manage/index/showback";
        }
        return url;
    }
}
